﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Diagnostics;
using common;

namespace common.web
{
    public class admin : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            chkRef();  chkLog();
            base.OnLoad(e);
            //sw.Stop();
            //Response.Write(sw.Elapsed);
        }

        public static string[] chkqx(string q)
        {
            string[] qx = admin.load();
            if (qx[0] == "1" || qx[1].Contains("," + q + ","))
            { }
            else
            {
                func.Alert("您没有操作权限!", null);
            }
            return qx;
        }
        public static string[] chkqx2(string q)
        {
            string[] qx = admin.load2();
            if (qx[0] == "1" || qx[1].Contains("," + q + ","))
            { }
            else
            {
                func.Alert("您没有操作权限!", null);
            }
            return qx;
        }

        public static void chkRef()
        {
            string ser_na = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            if (HttpContext.Current.Request.UrlReferrer == null || (HttpContext.Current.Request.UrlReferrer.Host != ser_na && HttpContext.Current.Request.UrlReferrer.Host != "chart.wsloan.com"))
            {
                    HttpContext.Current.Response.Write("<br><br><table width='80%' border='1' align='center' cellpadding='0' cellspacing='2' bordercolor='#99BADF'><tr><td height='30' align='center' bgcolor='#D8EAFA'><span style='font-size:13px'>你所访问的页面不存在,请从页面链接访问! <a href='#' onClick='javascript:window.close();'>〖关闭〗</a></span></td></tr></table>");
                    HttpContext.Current.Response.End();
            }
        }

        public static void chkLog()
        {
            bool islogin = true;
            if (chkSess() == false)
            {
                string[] id = uid();
                if (id[0] == "0")
                {
                    islogin = false;
                }
                else
                {
                    HttpContext.Current.Session.Timeout = 50; HttpContext.Current.Session["isok"] = "ok";
                }
            }
            if (islogin == false) func.Alert("已超时,请重新登录1!", "index.aspx");
        }

        public static string[] load()
        {
            //chkRef();
            string[] id = uid();
            if (id[0] == "0")
            {
                func.Alert("已超时,请重新登录2!", "index.aspx");
            }
            return id;
        }
        public static string[] load2()
        {
            string[] id = uid();
            if (id[0] == "0")
            {
                func.Alert("已超时,请重新登录3!", "index.aspx");
            }
            return id;
        }

        public static string[] uid()
        {
            string thuser1 = func.base64De(func.getCookis("infoo"));
            string thupas1 = func.getCookis("infot");
            string[] id = { "0", "" };
            if (inc.safestr(thuser1, "") == false || thupas1.Length != 32) return id;
            DataTable dt = db.dtTable("select id,cPass,cPurview from t_Manage where cUser='" + thuser1 + "'");
            if (dt.Rows.Count == 1 && chkPass(dt.Rows[0]["cPass"].ToString(), thupas1))
            {
                id[0] = dt.Rows[0]["id"].ToString();
                id[1] = dt.Rows[0]["cPurview"].ToString();
            }
            return id;
        }

        public static bool chkPass(string s, string tem)
        {
            s = func.PassStr(s + func.GetIP().Replace(".", ""), "MD5");
            if (s == tem) return true;
            return false;
        }

        private static bool chkSess()
        {
            if (HttpContext.Current.Session["isok"] == null || HttpContext.Current.Session["isok"].ToString() != "ok") return false;
            return true;
        }

        public static string url()
        {
            string _url = HttpContext.Current.Request.Url.PathAndQuery;
            return _url.Substring(_url.LastIndexOf("/") + 1);
        }
    }
}